package com.mydigitallife.timetracker;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class timetracker extends Activity {

	private final String MY_DATABASE_NAME = "TimeTrack";
	private final String MY_DATABASE_TABLE = "tracking";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SQLiteDatabase myDB = null;
		myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE,
				null);
		
		try {



			myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DATABASE_TABLE
					+ " (trackid INTEGER PRIMARY KEY , _id INTEGER, starttime DATETIME,"
					+ " endtime DATETIME);");
			myDB
					.execSQL("CREATE TABLE IF NOT EXISTS projects (_id INTEGER PRIMARY KEY , projectname VARCHAR(45))");			
		} catch (Exception e) {
			Log.e("Error", "Error", e);
		} finally {
			if (myDB != null)
				myDB.close();
		}
		setContentView(R.layout.main);
		
		
		final Button track = (Button) findViewById(R.id.button01);
		
		if (checkIfWorking()) {
			track.setText(R.string.stopbutton);
			track.setOnClickListener(new View.OnClickListener() {

				
				public void onClick(View v) {
					
						Cursor endTracking = connectDB().rawQuery("SELECT trackid FROM tracking ORDER BY trackid desc LIMIT 1;",null);
						connectDB().close();
						String workingProject = null;
						if(endTracking.moveToFirst()){
							workingProject = endTracking.getString(0);
						}

						if(checkIfWorking()){
							connectDB().execSQL("UPDATE tracking SET endtime=datetime() WHERE trackid="+ workingProject +";");
							connectDB().close();
							track.setText(R.string.workbutton);
							Toast.makeText(getApplicationContext(), "Tracking stopped", Toast.LENGTH_SHORT).show();
						} else {
							Intent myIntent = new Intent(v.getContext(), customers.class);
							startActivityForResult(myIntent, 0);
						}
						
				}
			});

		} else {		
			
		
		track.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
					Intent myIntent = new Intent(v.getContext(), customers.class);
					startActivityForResult(myIntent, 0);

			}
		});

		}
		
 
		
		Button overview = (Button) findViewById(R.id.Button02);
		overview.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent classSwitch = new Intent(v.getContext(), overview.class);
				startActivityForResult(classSwitch, 0);
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(this);	
		inflater.inflate(R.menu.menumain, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
		case R.id.help:
			popuphelp();
			return true;
		case R.id.about:
			popupabout();
			return true;
		}
		return false;
	}

	public SQLiteDatabase connectDB(){
		SQLiteDatabase myDB = null;
		myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
		return myDB;
	}
	
	public void removeAllProjects(){
		
		connectDB().execSQL("DELETE FROM projects");
		connectDB().close();
	}
	
	private void popuphelp(){
		final FrameLayout f1 = new FrameLayout(this);
		final TextView text = new TextView(this);
		text.setText(R.string.Help);
		text.setGravity(Gravity.CENTER);
		
		
		
		new AlertDialog.Builder(this)
			.setView(f1)
			.setTitle("Help")
			.setView(text)
			.create().show();
		
		new AlertDialog.Builder(this).setView(f1);
	}

	private void popupabout(){
		final FrameLayout f1 = new FrameLayout(this);
		final TextView input = new TextView(this);
		input.setText(R.string.about);
		input.setGravity(Gravity.CENTER);
		
		
		
		new AlertDialog.Builder(this)
			.setView(f1)
			.setTitle("About")
			.setView(input)
			.create().show();
		
		new AlertDialog.Builder(this).setView(f1);
	}
	
	private boolean checkIfWorking(){
		Cursor getRow = connectDB().rawQuery("SELECT endtime FROM tracking ORDER BY trackid desc LIMIT 1;", null);
		Cursor getId = connectDB().rawQuery("SELECT trackid FROM tracking ORDER BY trackid desc LIMIT 1;", null);
		connectDB().close();
		boolean checkifWorking = false;
		String rowContainer = null;
		if(getRow.moveToFirst()){
			do{
				rowContainer = getRow.getString(0);
			}while(getRow.moveToNext());
		}
		
		String idContainer = null;
		if(getId.moveToFirst()){
			do{
				idContainer = getId.getString(0);
			}while(getId.moveToNext());
		}
		
		if(rowContainer == null){
			if(idContainer != null){
			checkifWorking = true;
			}
		}
		
		return checkifWorking;
	}

}